package com.hrz.datasync.service.impl;

import com.hrz.datasync.common.RetResponse;
import com.hrz.datasync.common.RetResult;
import com.hrz.datasync.common.SnowflakeIdWorker;
import com.hrz.datasync.kafka.producer.BbqAdZoneProducer;
import com.hrz.datasync.pojo.primary.TpAdZone;
import com.hrz.datasync.pojo.secondary.BbqAdZone;
import com.hrz.datasync.repository.primary.TpAdZoneRepository;
import com.hrz.datasync.repository.secondary.BbqAdZoneRepository;
import com.hrz.datasync.service.BbqAdZoneService;
import com.hrz.datasync.service.TpAdZoneService;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @作  者: 侯建军
 * @日  期: 2018/11/05 17:02:50
 * @描  述:
 */
@Service
@Log
public class BbqAdZoneServiceImpl  implements BbqAdZoneService {
    @Autowired
    private BbqAdZoneRepository bbqAdZoneRepository;
    @Autowired
    private TpAdZoneRepository tpAdZoneRepository;
    @Autowired
    private BbqAdZoneProducer bbqAdZoneProducer;
    @Override
    @Transactional
    public RetResult addBbqAdZone(TpAdZone tpAdZone) {
        BbqAdZone bbqAdZone=this.tpAdZoneToBbqAdZone(tpAdZone);
        bbqAdZone.setSync(1);
        log.info("插入成功");
        if(tpAdZone.getSync()==0){
            bbqAdZoneRepository.save(bbqAdZone);
            //修改原数据状态
            tpAdZone.setSync(1);
            tpAdZoneRepository.saveAndFlush(tpAdZone);
        }
        log.info("TpAdZoneToBbqAdZone:"+bbqAdZone);
        return RetResponse.makeOKRsp();
    }
    /**
     *
     * 功能描述:
     * 发送消息
     * @参  数:
     * @返  回:
     * @作  者: 侯建军
     * @日  期: 2018/11/05 07:57:57
     */
    @Override
    public RetResult sendBbqAdZone(BbqAdZone bbqAdZone) {
        bbqAdZoneProducer.adZoneSend(bbqAdZone);
        return RetResponse.makeOKRsp();
    }

    /**
     *
     * 功能描述:
     * TpAdZone 转 BbqAdZone
     * @参  数:
     * @返  回:
     * @作  者: 侯建军
     * @日  期: 2018/11/05 05:08:12
     */
    public BbqAdZone tpAdZoneToBbqAdZone(TpAdZone tpAdZone) {
        BbqAdZone bbqAdZone = new BbqAdZone();
        //生成ID
        SnowflakeIdWorker idWorker = new SnowflakeIdWorker(0, 0);
        long id = idWorker.nextId();
        log.info(String.valueOf(id));
        bbqAdZone.setId(id);
        bbqAdZone.setTimeInsert(new Date(tpAdZone.getAddtime()));
        bbqAdZone.setIsUse((byte) 1);
        bbqAdZone.setDeleted(tpAdZone.getIsdelete());
        //通过adzone_name 进行拆分
        String[] strs=tpAdZone.getAdzoneName().split("_");
        if(strs.length>1){
            bbqAdZone.setUserId(new Long(strs[0]));
            bbqAdZone.setType(new Byte(strs[1]));
        }else{
            bbqAdZone.setUserId(new Long(strs[0]));
            //如果没有默认为1
            bbqAdZone.setType((byte)1);
        }
        bbqAdZone.setPidValid((byte) 1);
        bbqAdZone.setPid1(new Long(tpAdZone.getAlmmid()));
        bbqAdZone.setPid2(new Long(tpAdZone.getSiteid()));
        bbqAdZone.setPid3(new Long(tpAdZone.getAdzoneId()));

        bbqAdZone.setSelf(bbqAdZone.getSelf());
        //
        bbqAdZone.setAdzoneName(tpAdZone.getAdzoneName());
        //原始ID
        bbqAdZone.setOrginId(tpAdZone.getId());
        return bbqAdZone;
    }
}
